package de.velvettear.squirrel.gui;

import java.util.Date;

import net.sqlcipher.database.SQLiteDatabase;

import org.jivesoftware.smack.SmackAndroid;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;
import de.velvettear.squirrel.R;
import de.velvettear.squirrel.database.DatabaseHelper;
import de.velvettear.squirrel.kernel.Statics;
import de.velvettear.squirrel.util.AnimationHelper;
import de.velvettear.squirrel.util.ClipboardHelper;
import de.velvettear.squirrel.util.FileHelper;
import de.velvettear.squirrel.util.MessageHelper;
import de.velvettear.squirrel.util.SettingsHelper;
import de.velvettear.squirrel.util.WLANHelper;

public class Splash extends Activity {

	private TextView tv_message;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_splash);
	    
	    tv_message = (TextView) findViewById(R.id.splash_tv_message);
	    
	    new ClipboardHelper(getApplicationContext());
		new WLANHelper(getApplicationContext());
			    		
	    startApplication();
	}

	private void startApplication() {
		AsyncTask<Void, Void, Void> startApplicationTask = new AsyncTask<Void, Void, Void>() {

			@Override
			protected Void doInBackground(Void... params) {
				Log.i("startApplication()", "BEGIN");
				AnimationHelper.startAnimation(tv_message);
				Long startTime = new Date().getTime();
				
				guiHandler.sendMessage(MessageHelper.createMessage(Statics.HANDLER_SPLASH, "Initializing smack library ...", null));
				SmackAndroid.init(getApplicationContext());
				
				guiHandler.sendMessage(MessageHelper.createMessage(Statics.HANDLER_SPLASH, "Checking directory structure ...", null));
				FileHelper.createFolders();
											
				if (Statics.DATABASEHELPER == null || Statics.DATABASEHELPER .getWritableDatabase() == null) {
					try {		
						guiHandler.sendMessage(MessageHelper.createMessage(Statics.HANDLER_SPLASH, "Initializing sqlcipher library ...", null));
						SQLiteDatabase.loadLibs(getApplicationContext());
						Statics.DATABASEHELPER  = new DatabaseHelper(getApplicationContext(), Statics.PATH_DATABASE, Statics.DATABASE_VERSION);
						Statics.DATABASEHELPER.getWritableDatabase();
								
						guiHandler.sendMessage(MessageHelper.createMessage(Statics.HANDLER_SPLASH, "Initializing database ...", null));
						Statics.DATABASEHELPER.checkTables();		
						
						guiHandler.sendMessage(MessageHelper.createMessage(Statics.HANDLER_SPLASH, "Loading settings ...", null));
						SettingsHelper.createSettings();
						SettingsHelper.setDefaultSettings();
						
						guiHandler.sendMessage(MessageHelper.createMessage(Statics.HANDLER_SPLASH, "Creating gui ...", null));
						long diff = new Date().getTime() - startTime;
						if (diff < 3000) {
							long sleep = 3000 - diff; 
							Thread.sleep(sleep);
						}
					} catch (Exception e) {
						if (e.getMessage() != null) {
							Log.e("startApplication()", e.getMessage());					
						}
					}
				}
				
				Log.i("startApplication()", "END");
				return null;
			}
			
			@Override
			protected void onPostExecute(Void ret) {		
				AnimationHelper.stopAnimation(tv_message);
				startActivity(new Intent(Splash.this, MainFrame.class));
				finish();
			}
			
		};
		
		startApplicationTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}
	
	public Handler guiHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			Bundle data = msg.getData();
			if (data.containsKey(Statics.MESSAGE_SPLASH)) {
				tv_message.setText(data.getString(Statics.MESSAGE_SPLASH));
			}
		}
	};
	
}
